package cn.ava.pet.service.impl;

import cn.ava.basic.constant.BaseConstants;
import cn.ava.basic.exception.BusinessException;
import cn.ava.basic.utils.BaiDuAiCensorUtil;
import cn.ava.pet.domain.Pet;
import cn.ava.pet.domain.PetDetail;
import cn.ava.pet.domain.PetOnlineAuditLog;
import cn.ava.pet.mapper.PetDetailMapper;
import cn.ava.pet.mapper.PetMapper;
import cn.ava.pet.mapper.PetOnlineAuditLogMapper;
import cn.ava.pet.service.IPetService;
import cn.ava.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ava
 * @since 2022-07-16
 */
@Service
// 开启事务 只读属性和支持事务
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    /**
     * @Title: delete
     * @Description: 删除宠物数据和详情信息
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/26 16:44
     * @param:
     * @return:
     */
    @Override
    @Transactional
    public void delete(Long id) {
        Pet pet = petMapper.getById(id);
        petMapper.delete(id);
        // 删除对应的详情信息
        petDetailMapper.deleteByPetId(pet.getId());
    }

    /**
     * @Title: add
     * @Description: 添加宠物数据时关联添加详情信息
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/26 16:45
     * @param:
     * @return:
     */
    @Override
    @Transactional
    public void add(Pet pet) {
        // 保存
        petMapper.add(pet);
        // 获取到详情信息
        PetDetail petDetail = pet.getDetail();
        // 判断非空
        if (!StringUtils.isEmpty(petDetail)){
            // 设置petId
            petDetail.setPetId(pet.getId());
            // 保存
            petDetailMapper.add(petDetail);
        }
    }

    /**
     * @Title: update
     * @Description: 同步修改信息
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/26 16:46
     * @param:
     * @return:
     */
    @Override
    @Transactional
    public void update(Pet pet) {
        // 修改
        petMapper.update(pet);
        PetDetail petDetail = pet.getDetail();
        // 先直接删除对应的详情信息
        petDetailMapper.deleteByPetId(pet.getId());
        if (!StringUtils.isEmpty(petDetail)){
            // 设置id
            petDetail.setPetId(pet.getId());
            // 再保存一次
            petDetailMapper.add(petDetail);
        }
    }

    /**
     * @Title: offsale
     * @Description: 批量下架
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/16 22:21
     * @param:
     * @return:
     */
    @Override
    public void offsale(List<Long> ids) {
        petMapper.offsale(ids);
    }
    /**
     * @Title: onsale
     * @Description: 批量上架
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/16 22:21
     * @param:
     * @return:
     */
    @Override
    public void onsale(List<Long> ids) {
        // 循环数组修改
        for (Long id : ids) {
            // 查询出对应的数据
            Pet pet = petMapper.getById(id);
            // 空字符串
            StringBuilder sb = new StringBuilder();
            // 审核判断是否有违规
            if (!BaiDuAiCensorUtil.TextCensor(pet.getName())){
                sb.append("宠物名字违规,违规信息: ").append(pet.getName());
            }
            // 循环分割出图片路径
            for (String Images : pet.getResources().split(",")) {
                // 进行审核判断
                if (!BaiDuAiCensorUtil.ImgCensor(BaseConstants.VerifyShopSettlement.FASTDFS_IMAGE + Images)) {
                    sb.append("宠物图片违规,违规信息: ").append(BaseConstants.VerifyShopSettlement.FASTDFS_IMAGE).append(Images);
                }
            }
            // 如果大于0,说明有违规信息,那么保存日志信息
            if (sb.length() > 0) {
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(pet.getId());
                petOnlineAuditLog.setState(0);
                petOnlineAuditLog.setNote(sb.toString());
                petOnlineAuditLog.setAuditTime(new Date());
                petOnlineAuditLogMapper.add(petOnlineAuditLog);
                continue;
            }
            // 修改状态
            petMapper.onsale(id);
        }
    }

    /**
     * @Title: getPetDetail
     * @Description: 获取宠物详情信息
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/26 16:48
     * @param:
     * @return:
     */
    @Override
    public PetDetail getPetDetail(Long id) {
        return petMapper.getPetDetail(id);
    }
}
